package cms

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"seawolf-admin-go/internal/app/admin/controller"
	"seawolf-admin-go/internal/request/BaseReq"
	"seawolf-admin-go/internal/service/SlidesService"
	"seawolf-admin-go/utility/response"
)

type cSlides struct {
	*controller.BaseController
}

func NewSlidesController(group *ghttp.RouterGroup, defaultVersion string) {
	// 初始化内部变量
	inputReq := &BaseReq.I{}
	c := &cSlides{&controller.BaseController{Req: inputReq}}
	c.New(group, defaultVersion)
}

func (c *cSlides) New(group *ghttp.RouterGroup, defaultVersion string) {
	var leftJoin []*BaseReq.QueryOpLeftJoin
	prefixTmp, err := g.Cfg().Get(context.Background(), "database.prefix")
	if err != nil {
		return
	}
	dbPrefix := prefixTmp.String()
	leftJoin = append(leftJoin, &BaseReq.QueryOpLeftJoin{
		TableInfo: dbPrefix + "category c",
		Condition: "a.category_id = c.id",
	})
	// 配置表名
	c.Req = &BaseReq.I{
		TableName:          "slides",
		InfoIgnoreProperty: "delete_at",
		SoftDeleteField:    "delete_at",
		PageQueryOp: &BaseReq.QueryOp{
			KeyWordLikeFields: []string{
				"a.title",
			},
			SelectFields: g.ArrayStr{
				"a.*",
				"c.cat_name",
			},
			AsName:   "a",
			LeftJoin: leftJoin,
			OtherWhere: func(r *ghttp.Request) g.Map {
				data := g.Map{}
				categoryIds := r.Get("categoryIds").Array()
				if len(categoryIds) > 0 {
					data["category_id IN (?)"] = categoryIds
				}

				return data
			},
		},
		ListQueryOp: &BaseReq.QueryOp{
			KeyWordLikeFields: []string{
				"title",
			},
		},
	}
	// 登录中间件
	// 权限判断中间件
	// 处理初始化方法和结束方法中间件
	group.Middleware(func(r *ghttp.Request) {
		c.Init(r)
		r.Middleware.Next()
		c.Shut(r)
	})
	// 注册基础路由
	controller.RegBase(group, c, nil)
}

func (c *cSlides) Info(r *ghttp.Request) *response.JsonResponse {
	c.Req.Id = r.Get("id", "").String()
	if c.Req.Id == "" {
		return response.FailByRequestMessage(r, "id不能为空")
	}
	return SlidesService.Info(r, c.Req)
}
